<template>
  <div class="calendar-container">
    <BackButton />
    <div class="calendar">
      <div class="calendar-header">
        <button @click="previousMonth">&lt;</button>
        <span>{{ currentYearMonth }}</span>
        <button @click="nextMonth">&gt;</button>
      </div>
      <div class="calendar-weekdays">
        <div v-for="day in weekDays" :key="day" class="weekday">{{ day }}</div>
      </div>
      <div class="calendar-days">
        <div
          v-for="day in days"
          :key="day.date"
          :class="['calendar-day', { 'current-month': day.currentMonth, 'today': day.isToday }]"
          @click="selectDate(day)"
        >
          {{ day.dayNumber }}
          <div v-if="day.hasCheckIn" class="check-in-mark">✓</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import BackButton from './BackButton.vue'

export default {
  name: 'Calendar',
  components: {
    BackButton
  },
  data() {
    return {
      currentDate: new Date(),
      selectedDate: null,
      weekDays: ['日', '一', '二', '三', '四', '五', '六']
    }
  },
  computed: {
    currentYearMonth() {
      return `${this.currentDate.getFullYear()}年${this.currentDate.getMonth() + 1}月`
    },
    days() {
      const year = this.currentDate.getFullYear()
      const month = this.currentDate.getMonth()
      const firstDay = new Date(year, month, 1)
      const lastDay = new Date(year, month + 1, 0)
      
      const days = []
      const today = new Date()
      
      // 填充上个月的日期
      const prevMonthDays = firstDay.getDay()
      const prevMonth = new Date(year, month, 0)
      for (let i = prevMonthDays - 1; i >= 0; i--) {
        days.push({
          date: new Date(year, month - 1, prevMonth.getDate() - i),
          dayNumber: prevMonth.getDate() - i,
          currentMonth: false,
          isToday: false
        })
      }
      
      // 当前月的日期
      for (let i = 1; i <= lastDay.getDate(); i++) {
        const date = new Date(year, month, i)
        days.push({
          date,
          dayNumber: i,
          currentMonth: true,
          isToday: date.toDateString() === today.toDateString(),
          hasCheckIn: false // 这里可以根据实际打卡数据设置
        })
      }
      
      // 填充下个月的日期
      const remainingDays = 42 - days.length // 保持6行日历
      for (let i = 1; i <= remainingDays; i++) {
        days.push({
          date: new Date(year, month + 1, i),
          dayNumber: i,
          currentMonth: false,
          isToday: false
        })
      }
      
      return days
    }
  },
  methods: {
    previousMonth() {
      this.currentDate = new Date(this.currentDate.getFullYear(), this.currentDate.getMonth() - 1)
    },
    nextMonth() {
      this.currentDate = new Date(this.currentDate.getFullYear(), this.currentDate.getMonth() + 1)
    },
    selectDate(day) {
      this.selectedDate = day.date
      this.$emit('date-selected', day.date)
    }
  }
}
</script>

<style scoped>
.calendar-container {
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.calendar {
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  background-color: #fff;
}

.calendar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px;
  background-color: #4f46e5;
  color: white;
}

.calendar-header button {
  background: none;
  border: none;
  color: white;
  font-size: 18px;
  cursor: pointer;
  padding: 5px 10px;
  border-radius: 4px;
}

.calendar-header button:hover {
  background-color: rgba(255, 255, 255, 0.2);
}

.calendar-weekdays {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  background-color: #f5f5f5;
  border-bottom: 1px solid #e0e0e0;
}

.weekday {
  padding: 10px;
  text-align: center;
  font-weight: bold;
  color: #666;
}

.calendar-days {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  grid-template-rows: repeat(6, 1fr);
}

.calendar-day {
  position: relative;
  height: 40px;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  color: #999;
  border-bottom: 1px solid #f0f0f0;
  border-right: 1px solid #f0f0f0;
}

.calendar-day:nth-child(7n) {
  border-right: none;
}

.calendar-day.current-month {
  color: #333;
}

.calendar-day.today {
  background-color: #e6f7ff;
  font-weight: bold;
}

.calendar-day:hover {
  background-color: #f0f0f0;
}

.check-in-mark {
  position: absolute;
  bottom: 2px;
  right: 2px;
  width: 12px;
  height: 12px;
  background-color: #50C9A3;  /* 修改为薄荷绿 */
  color: white;
  border-radius: 50%;
  font-size: 8px;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>